import { HttpService } from "@nestjs/axios";
import { Cat } from "src/common/types/cats.type";
import { catchError, firstValueFrom } from "rxjs";
import { Injectable, Logger } from "@nestjs/common";
import { HTTP_CATS_API_URL } from "src/constant/config.const";

@Injectable()
export class HttpCatsService {
    constructor(private readonly httpService: HttpService) { }
    private readonly logger = new Logger(HttpCatsService.name);

    async findAll(): Promise<Cat[]> {
        const { data } = await firstValueFrom(
            this.httpService.get<Cat[]>(HTTP_CATS_API_URL).pipe(
                catchError((error) => {
                    this.logger.error(error.response.data);
                    throw new Error(error.message);
                })
            )
        );
        return data;
    }
}